-
Notifications
You must be signed in to change notification settings - Fork 657
fix(rome_js_formatter): print empty statements as empty blocks #2504
Conversation
Parser conformance results on ubuntu-latestjs/262
jsx/babel
ts/babel
ts/microsoft
|
Deploying with Cloudflare Pages
|
358eec3
to
802baa2
Compare
802baa2
to
f02529d
Compare
|
||
impl FormatNode for JsEmptyStatement { | ||
fn format_fields(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let JsEmptyStatementFields { semicolon_token } = self.as_fields(); | ||
|
||
Ok(formatter.format_replaced(&semicolon_token?, empty_element())) | ||
let parent_kind = self.syntax().parent().map(|p| p.kind()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: this pattern shows up a couple times. Should we have a function that gets the parent kind directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When it comes to unifying the printing behavior of empty statements, I would suggest we go the opposite way and extend the behavior of if
statements (always insert a block) to all other affected statements (for
loops, for in
loops, for of
loops, while
loops, do while
loops and with
blocks).
The reasoning behind this is that Rome JS had a useBlockStatements
lint rule that emitted a fixable warning for these nodes if they were using a single statement as their body, but for this kind of "style fix" we might as well rewrite it directly in the formatter.
Does that mean that we want to intentionally diverge from prettier? |
Well at least I think we should discuss whether we want to have code-style related lint rules in the analyzer or to enforce these decisions in the formatter directly. Ultimately the decision is probably about how disruptive the change is (how often are these single-statement syntax actually used) vs. how confusing it is for users to see fixable diagnostics in their code for things they'd expect the formatter to take care of. |
I completely forgot about that lint rule. Yes, in Rome classic there was this idea where "coding style rules" should not exists because we own the formatter too. This is the rule FYI: https://github.com/rome/tools/blob/archived-js/internal/compiler/lint/rules/js/useBlockStatements.test.md At this point I think we should just diverge from prettier. I agree with you @leops |
333a6e2
to
c7aa984
Compare
I don't think we should. This is a style decision, and we should have a very high bar for where we diverge from Prettier purely for style reasons (rather than implementation complexity). We don't necessarily need to add every lint rule that existed in Rome classic. But if we do end up with some "code style rules", I think that's ok because we've intentionally relinquished some of our control over the formatting output by targeting Prettier equivalence. |
@yassere Could let us know why though? Why you think we should not put blocks and why we don't need that lint rule anymore. I think it makes sense to have blocks. Having empty statements can lead to errors, same for expressions without blocks. |
Personally I view diverging from Prettier as a large enough reason. If we get far enough in linting that we create this rule again, we can consider diverging then. But until that time, we should do what's best for the formatter, and not what might be best for the linter. IMO, as long as we're not seriously inhibiting future tools, we should do what's best for the tools that currently exist. Each of our tools needs to gain adoption and the promise of a linter feature does not outweigh breaking prettier compatibility, at least for me. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ematipico I think it makes sense to have blocks too. Maybe that's something we can have as a safe autofix for the linter. But for the formatter, I think we need to respect our commitment to match Prettier's styling decisions as much as possible, unless we have a very good reason not to.
Given our previous messaging, intentionally deviating from Prettier's style is not a decision we should make lightly. Each case should be discussed in its own issue so that we can make a strong case to justify that divergence and document it.
Summary
Fixes #2448
Test Plan
Added new cases, one for the
if
and one fordo while